package com.yanqu.road.dao.impl.activity.carptrip;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.carptrip.data.CarpTripPvPCellData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarpTripPvPCellDataDaoImpl extends TempDataObjectDao<CarpTripPvPCellData> {

    public static String getCarpTripPvPCellDataSql = "select * from `t_c_carp_trip_pvp_data` where `activityId`=? order by cellId";
    public List<CarpTripPvPCellData> getCarpTripPvPCellData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CarpTripPvPCellData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCarpTripPvPCellDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CarpTripPvPCellData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String insertCarpTripPvPCellDataSql = "insert into `t_c_carp_trip_pvp_data`(activityId,cellId,blood,userId,userBaseInfo) values (?,?,?,?,?)";

    @Override
    protected boolean addData(CarpTripPvPCellData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserBaseInfo() == null ? "" : JSON.toJSONString(data.getUserBaseInfo())));
        if (getDb().executeNoneQuery(insertCarpTripPvPCellDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCarpTripPvPCellDataSql = "update `t_c_carp_trip_pvp_data` set blood=?,userId=?,userBaseInfo=? where activityId=? and cellId=?";

    @Override
    protected boolean updateData(CarpTripPvPCellData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUserBaseInfo() == null ? "" : JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCellId()));
        if (getDb().executeNoneQuery(updateCarpTripPvPCellDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CarpTripPvPCellData parseData(ResultSet rs) throws SQLException {
        CarpTripPvPCellData carpTripPvPCellData = new CarpTripPvPCellData();
        carpTripPvPCellData.setActivityId(rs.getInt("activityId"));
        carpTripPvPCellData.setCellId(rs.getInt("cellId"));
        carpTripPvPCellData.setBlood(rs.getString("blood"));
        carpTripPvPCellData.setUserId(rs.getLong("userId"));
        String userBaseInfo = rs.getString("userBaseInfo");
        if(!StringUtils.isNullOrEmpty(userBaseInfo)){
            carpTripPvPCellData.setUserBaseInfo(JSON.parseObject(userBaseInfo, UserBaseInfo.class));
        }
        return carpTripPvPCellData;
    }
}
